package com.wish.group05.base.service.impl;

import com.wish.group05.base.dto.JrFactoringRequestDto;
import com.wish.group05.base.dto.JrFactoringResponseDto;
import com.wish.group05.base.entity.CfContract;
import com.wish.group05.base.entity.CwReceiptPlan;
import com.wish.group05.base.entity.JrFactoringDetail;
import com.wish.group05.base.entity.JrProduct;
import com.wish.group05.base.mapper.JrFactoringDetailMapper;
import com.wish.group05.base.service.ICfContractService;
import com.wish.group05.base.service.ICwReceiptPlanService;
import com.wish.group05.base.service.IJrFactoringDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.group05.base.service.IJrProductService;
import com.wish.group05.base.vo.CatulateFactorInfoVo;
import com.wish.group05.base.vo.RequestRemainingLeavingBillVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author houzehui
 * @since 2022-05-23
 */
@Service
public class JrFactoringDetailServiceImpl extends ServiceImpl<JrFactoringDetailMapper, JrFactoringDetail> implements IJrFactoringDetailService {


    @Autowired
    ICfContractService cfContractService;

    @Autowired
    ICwReceiptPlanService iCwReceiptPlanService;

    @Autowired
    IJrProductService iJrProductService;

    /**
     * 根据合同和保理产品计算该合同的保理申请明细
     */
    @Override
    public JrFactoringDetail catulateFactorInfo(CatulateFactorInfoVo catulateFactorInfoVo) {
    //根据申请保理的租房合同ID查询
        CfContract cfContract = cfContractService.getById(catulateFactorInfoVo.getId());
        //创建对象
        JrFactoringDetail jrFactoringDetail = new JrFactoringDetail();
        //复制数据
        BeanUtils.copyProperties(cfContract, jrFactoringDetail);
        jrFactoringDetail.setCfContractId(catulateFactorInfoVo.getId());
        // 查询剩余租约期限（未付款的房租账期） 剩余租约金房租合计
        RequestRemainingLeavingBillVo requestRemainingLeavingBillVo = new RequestRemainingLeavingBillVo();
        //赋值申请保理的租房合同ID  1
        requestRemainingLeavingBillVo.setContractId(catulateFactorInfoVo.getId());
        System.err.println(catulateFactorInfoVo.getId());
        //赋值状态  2
        requestRemainingLeavingBillVo.setContractType(2);

        List<CwReceiptPlan> cwReceiptPlans = iCwReceiptPlanService.selectRemainingLeaseBill(requestRemainingLeavingBillVo);
        System.err.println(cwReceiptPlans);

        //判断是否为空
        if(cwReceiptPlans!=null && cwReceiptPlans.size()>0){
            // 借款账期开始日
            jrFactoringDetail.setBorrowedStart(cwReceiptPlans.get(0).getPaymentDaysBegin());
            // 借款账期结束日
            jrFactoringDetail.setBorrowedEnd(cwReceiptPlans.get(cwReceiptPlans.size() - 1).getPaymentDaysEnd());
            // 应收租约金额：即剩余租约金房租合计
            BigDecimal shouldMonthRent = BigDecimal.ZERO;
            for (CwReceiptPlan cwReceiptPlan : cwReceiptPlans) {
                shouldMonthRent=shouldMonthRent.add(cwReceiptPlan.getPaymentAmount());
            }
            jrFactoringDetail.setShouldMonthRent(shouldMonthRent);
            // 借款周期(多少个月 = 付几 * 剩余未付房租账期数)
            jrFactoringDetail.setFactoringPeriod(cfContract.getPaymentType() * cwReceiptPlans.size());

            //所选的保理产品ID
            JrProduct jrProduct = iJrProductService.getById(catulateFactorInfoVo.getJrProductId());
            // 授信金额 = 应收租约金额*(授信额度/100) (备注：四舍五入)
            jrFactoringDetail.setFactoringCreditMoney(shouldMonthRent.multiply(new BigDecimal(String.valueOf(jrProduct.getCreditLine())).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP)).setScale(2, BigDecimal.ROUND_HALF_UP));
            // 利息 = 保理授信总金额 *月利率 *月数  = 保理授信总金额 * 年利率/12 *月数 (备注：四舍五入)
            jrFactoringDetail.setFactoringInterest(jrFactoringDetail.getFactoringCreditMoney().multiply(new BigDecimal(catulateFactorInfoVo.getRate()/(100 * 12))).multiply(new BigDecimal(jrFactoringDetail.getFactoringPeriod())).setScale(2, BigDecimal.ROUND_HALF_UP));
            // 服务费 = 保理授信金额*服务费率/12*月数(备注：四舍五入)
            jrFactoringDetail.setServiceFee(jrFactoringDetail.getFactoringCreditMoney().multiply(new BigDecimal(catulateFactorInfoVo.getServiceFee()/(100 * 12))).multiply(new BigDecimal(jrFactoringDetail.getFactoringPeriod())).setScale(2, BigDecimal.ROUND_HALF_UP));


        }

        return jrFactoringDetail;
    }

    @Override
    public List<JrFactoringResponseDto> getJrFactoringDetailList(JrFactoringRequestDto params) {
        return getBaseMapper().getJrFactoringDetailList(params);
    }
}
